package com.lineage.server.command.executor;

import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.model.L1Teleport;
import com.lineage.server.serverpackets.S_NPCTalkReturn;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.S_SkillSound;
import com.lineage.server.serverpackets.S_SystemMessage;
import com.lineage.server.serverpackets.ServerBasePacket;
import com.lineage.server.templates.L1Account;
import com.lineage.server.thread.GeneralThreadPool;
import com.lineage.server.utils.Teleportation;
import com.lineage.server.world.World;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class L1ToPC2 implements L1CommandExecutor {
  private static final Log _log = LogFactory.getLog(L1ToPC2.class);
  
  private static final SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm");
  
  private static final int PAGE_SIZE = 12;
  
  private static List<L1PcInstance> _all_players;
  
  public static L1CommandExecutor getInstance() {
    return new L1ToPC2();
  }
  
  public void execute(L1PcInstance pc, String cmdName, String arg) {
    try {
      L1PcInstance target = World.get().getPlayer(arg);
      if (target != null) {
        pc.set_showId(target.get_showId());
        L1Teleport.teleport(pc, target.getX(), target.getY(), target.getMapId(), pc.getHeading(), false);
        pc.sendPackets((ServerBasePacket)new S_SystemMessage("移动至指定人物身边: " + arg));
      } else {
        checkTPhtml(pc, 0);
      } 
    } catch (Exception e) {
      _log.error("错误的GM指令格式: " + getClass().getSimpleName() + " 执行的GM:" + pc.getName());
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(261));
    } 
  }
  
  public static final void checkTPhtml(L1PcInstance pc, int page) {
    List<L1PcInstance> players = _all_players;
    if (players == null) {
      pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(pc.getId(), "topcscreen"));
      return;
    } 
    int length = players.size();
    int size = length / 12 + ((length % 12 != 0) ? 1 : 0);
    if (page < 0 || page >= size)
      return; 
    pc.get_other().set_page(page);
    String[] html_data = new String[13];
    html_data[0] = String.valueOf(String.valueOf(page + 1)) + "/" + size;
    int index = 1;
    int i = page * 12;
    int end = i + 12;
    while (i < end && i < length) {
      L1PcInstance player = World.get().getPlayer(((L1PcInstance)players.get(i)).getName());
      if (player != null) {
        L1Account acc = player.getNetConnection().getAccount();
        if (acc != null)
          html_data[index] = String.valueOf(String.valueOf(player.getName())) + " (Lv." + player.getLevel() + ")[" + 
            player.getClanname() + "]"; 
      } 
      i++;
      index++;
    } 
    pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(pc.getId(), "topcscreen", html_data));
  }
  
  public static final void checkTPhtmlPredicate(L1PcInstance pc, int page, boolean refreshAll) {
    List<L1PcInstance> all_players = new ArrayList<>();
    if (refreshAll) {
      Iterator<L1PcInstance> iterator = World.get().getAllPlayers().iterator();
      while (iterator.hasNext()) {
        L1PcInstance tgpc = iterator.next();
        if (tgpc.getMap().isUsableShop() == 0 && tgpc.getMapId() != 5143 && tgpc.getMapId() != 5301 && 
          tgpc.getMapId() != 5300)
          all_players.add(tgpc); 
      } 
    } else {
      Iterator<L1PcInstance> iterator2 = World.get().getAllPlayers().iterator();
      while (iterator2.hasNext()) {
        L1PcInstance tgpc = iterator2.next();
        if (tgpc.getMapId() == pc.getMapId())
          all_players.add(tgpc); 
      } 
    } 
    _all_players = all_players;
    List<L1PcInstance> players = _all_players;
    if (players == null) {
      pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(pc.getId(), "topcscreen"));
      return;
    } 
    int length = players.size();
    int size = length / 12 + ((length % 12 != 0) ? 1 : 0);
    if (page < 0 || page >= size)
      return; 
    pc.get_other().set_page(page);
    String[] html_data = new String[13];
    html_data[0] = String.valueOf(String.valueOf(page + 1)) + "/" + size;
    int index = 1;
    int i = page * 12;
    int end = i + 12;
    while (i < end && i < length) {
      L1PcInstance player = World.get().getPlayer(((L1PcInstance)players.get(i)).getName());
      if (player != null) {
        L1Account acc = player.getNetConnection().getAccount();
        if (acc != null)
          html_data[index] = String.valueOf(String.valueOf(player.getName())) + " (Lv." + player.getLevel() + ")[" + 
            player.getClanname() + "]"; 
      } 
      i++;
      index++;
    } 
    pc.sendPackets((ServerBasePacket)new S_NPCTalkReturn(pc.getId(), "topcscreen", html_data));
  }
  
  public static final void teleport2Player(final L1PcInstance pc, int index) {
    if (_all_players == null) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("请点选(刷新)选项。"));
      return;
    } 
    int point = pc.get_other().get_page() * 12 + index;
    if (point < 0 || point >= _all_players.size()) {
      pc.sendPackets((ServerBasePacket)new S_SystemMessage("玩家列表被变动过，请重新输入(.topc)来取得目前列表。"));
      return;
    } 
    L1PcInstance target = _all_players.get(point);
    if (target != null) {
      final L1PcInstance player = World.get().getPlayer(target.getName());
      if (player != null) {
        GeneralThreadPool.get().schedule(new Runnable() {
              public void run() {
                pc.set_showId(player.get_showId());
                pc.sendPacketsX8((ServerBasePacket)new S_SkillSound(pc.getId(), 2236));
                pc.setTeleportX(player.getX());
                pc.setTeleportY(player.getY());
                pc.setTeleportMapId(player.getMapId());
                Teleportation.teleportation(pc);
                pc.sendPackets((ServerBasePacket)new S_SystemMessage("移动至指定人物身边: " + player.getName()));
                L1ToPC2.checkTPhtml(pc, pc.get_other().get_page());
              }
            },440L);
        return;
      } 
    } 
    pc.sendPackets((ServerBasePacket)new S_SystemMessage("该人物不存在或已离线。"));
  }
}
